﻿
using Newtonsoft.Json;
using NWMES;
using NWMES.Mes;
using NWMES.MiModuleAutoInSetServiceService;
using System;
using System.Configuration;
using System.Net;

namespace MyMes.Mes
{
    public class MesMiModuleAutoInSetService : MESBaseService
    {
        public static NetworkCredential networkCredential = null;
        static MesMiModuleAutoInSetService Params = null;
        static string DefaultApiSection = "MesMiModuleAutoInSetService";

        static MesMiModuleAutoInSetService()
        {
            Params = config[DefaultApiSection].ToObject<MesMiModuleAutoInSetService>();
            networkCredential = new NetworkCredential(Params.UserName, Params.Password);
        }
       
        /// <summary>
        /// 释放套号
        /// </summary>
        /// <param name="invBarcodeBuff"></param>
        /// <param name="strError"></param>
        /// <returns></returns>
        public MesResult RequestMes(string[] invBarcodeBuff, string ApiSection)
        {
            MiModuleAutoInSetServiceService miModuleAutoInSetServiceService;
            miAutoSet miAutoSet;
            if (!string.IsNullOrEmpty(ApiSection))
            {
                Params = config[ApiSection].ToObject<MesMiModuleAutoInSetService>();
                networkCredential = new NetworkCredential(Params.UserName, Params.Password);
            }
            else
            {
                ApiSection = DefaultApiSection;
            }
            string ErrorMessage = Params.ParameterVerification();
            if (!string.IsNullOrEmpty(ErrorMessage))
            {
                return new MesResult(-99, ErrorMessage);
            }
            SetRequestParams(invBarcodeBuff, out miModuleAutoInSetServiceService, out miAutoSet);

            MesResult result = MesResult.InitMesResult(JsonConvert.SerializeObject(miAutoSet));

            miAutoSetResponse Response = new miAutoSetResponse();
            try
            {
                Response = miModuleAutoInSetServiceService.miAutoSet(miAutoSet);

                result.EndTime = DateTime.Now;
                var ret = Response.@return;
                result.Message = ret?.message;
                result.Code = ret.code;
                result.Success = result.Code == 0;
                result.ResponeJson = JsonConvert.SerializeObject(Response);

                result.Respone = Response;
                result.ret = ret;
            }
            catch (Exception ex)
            {
                result.Code = -99;
                result.Message = ex.Message;

            }
            SaveMesLog("MesMiModuleAutoInSetService", result);
            return result;

        }

        private static void SetRequestParams(string[] invBarcodeBuff, out MiModuleAutoInSetServiceService miModuleAutoInSetServiceService, out miAutoSet miAutoSet)
        {
            miModuleAutoInSetServiceService = new MiModuleAutoInSetServiceService();
            miModuleAutoInSetServiceService.Url = Params.Url;
            miModuleAutoInSetServiceService.Timeout = Params.Timeout;
            miModuleAutoInSetServiceService.Credentials = networkCredential;


            miModuleAutoInSetRequest MiModuleAutoInSetRequest = new miModuleAutoInSetRequest();
            MiModuleAutoInSetRequest.site = Params.Site;
            MiModuleAutoInSetRequest.userID = Params.User;
            MiModuleAutoInSetRequest.operation = Params.Operation;
            //  MiModuleAutoInSetRequest.operationRev = OperationVension;
            //MiModuleAutoInSetRequest.setNo = OperationVension;
            MiModuleAutoInSetRequest.activityID = Params.ActivityId;
            MiModuleAutoInSetRequest.resource = Params.Resource;
            MiModuleAutoInSetRequest.modeRel = bool.Parse(Params.modeRel);
            MiModuleAutoInSetRequest.projectBom = Params.projectBom;

            // MiModuleAutoInSetRequest.invBarcode = new string[invBarcodeBuff.Length];
            MiModuleAutoInSetRequest.invBarcode = invBarcodeBuff;

            miAutoSet = new miAutoSet();
            miAutoSet.ModuleSetRequest = MiModuleAutoInSetRequest;
        }

        /// <summary>
        /// 释放套号
        /// </summary>
        public string modeRel { get; private set; } = "true";

        /// <summary>
        /// 系统
        /// </summary>
        public string projectBom { get; private set; } = "Z3-EB401(986)";


    }
}
